/*
 * KP ebdesk
 */

package com.ebdesk.controller.masterdata;

import com.ebdesk.model.masterdata.Printer;
import com.ebdesk.model.masterdata.database.DatabaseData;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Zulfikar <zulfikarfull@gmail.com>
 */
public class PrinterManagement {
    private DatabaseData databaseData;
    private String tablePrinter = "pbb.pbb_sppt_printer";
    private int totalRow;

    public PrinterManagement() throws ClassNotFoundException, IOException, SQLException {
        databaseData = new DatabaseData();
    }

    public List<Printer> getAllPrinter(int pageNumber, int rowsPerPage) throws SQLException {
        ResultSet rs = getDatabaseData().queryData("select * from " + getTablePrinter() +
                " limit " + rowsPerPage + " offset " + ((pageNumber - 1) * rowsPerPage));

        List<Printer> dataPrinter = new ArrayList<Printer>();
        
        ResultSet rst = getDatabaseData().queryData("select count(*) from " + getTablePrinter());
        while (rst.next()) {
            totalRow = rst.getInt(1);
        }
        
        while(rs.next()){
            Printer printer = new Printer();

            printer.setIp_address_printer(rs.getString(1));
            printer.setNama_printer(rs.getString(2));
            printer.setId(rs.getInt(3));

            dataPrinter.add(printer);
        }

        return dataPrinter;

    }

    public List<Printer> SearchingPrinterByIp(String ip_address_printer, int pageNumber, int rowsPerPage) throws SQLException {
        ResultSet rs = getDatabaseData().queryData("select * from " + getTablePrinter() + " where ip_address_printer like '%" + ip_address_printer.trim() + "%'" +
                " limit " + + rowsPerPage + " offset " + ((pageNumber - 1) * rowsPerPage));

        List<Printer> dataPrinter = new ArrayList<Printer>();
        
        ResultSet rst = getDatabaseData().queryData("select count(*) from " + getTablePrinter() + " where ip_address_printer like '%" + ip_address_printer.trim() + "%'");
        while (rst.next()) {
            totalRow = rst.getInt(1);
        }
        while(rs.next()){
            Printer printer = new Printer();

            printer.setIp_address_printer(rs.getString(1));
            printer.setNama_printer(rs.getString(2));
            printer.setId(rs.getInt(3));

            dataPrinter.add(printer);
        }

        return dataPrinter;

    }
    
    public List<Printer> SearchingPrinterByNama(String nama_printer, int pageNumber, int rowsPerPage) throws SQLException {
        ResultSet rs = getDatabaseData().queryData("select * from " + getTablePrinter() + " where nama_printer like '%" + nama_printer + "%'" +
                " limit " + + rowsPerPage + " offset " + ((pageNumber - 1) * rowsPerPage));

        List<Printer> dataPrinter = new ArrayList<Printer>();
        ResultSet rst = getDatabaseData().queryData("select count(*) from " + getTablePrinter() + " where nama_printer like '%" + nama_printer + "%'");
        while (rst.next()) {
            totalRow = rst.getInt(1);
        }
        
        while(rs.next()){
            Printer printer = new Printer();

            printer.setIp_address_printer(rs.getString(1));
            printer.setNama_printer(rs.getString(2));
            printer.setId(rs.getInt(3));

            dataPrinter.add(printer);
        }

        return dataPrinter;

    }


    public Printer getPrinter(int id) throws SQLException {
        ResultSet rs = getDatabaseData().queryData("select * from " + getTablePrinter() + " where id = '" + id + "'");

        Printer printer = new Printer();
        while (rs.next()) {
            printer.setIp_address_printer(rs.getString(1));
            printer.setNama_printer(rs.getString(2));
            printer.setId(rs.getInt(3));
        }
        return printer;
    }

    public int addPrinter(Printer printer) throws SQLException {
        try {
            String sql = "insert into " + getTablePrinter() + "(ip_address_printer, nama_printer) "+
                "values(?,?)";

            PreparedStatement ps = getDatabaseData().getPreparedStatement(sql);

            ps.setString(1, printer.getIp_address_printer());
            ps.setString(2, printer.getNama_printer());

            return getDatabaseData().updateData(ps);

        } catch (org.postgresql.util.PSQLException ex) {
            System.out.println("ga boleh!, username sudah ada");
            return -1;
        }

    }

    public int removePrinter(Printer printer) throws SQLException {
        String sql = "delete from " + getTablePrinter() + " where id = '" + printer.getId() + "'";

        PreparedStatement ps = getDatabaseData().getPreparedStatement(sql);

        return getDatabaseData().updateData(sql);
    }

    public int removePrinter(int id) throws SQLException {
        String sql = "delete from " + getTablePrinter() + " where id = '" + id + "'";

        PreparedStatement ps = getDatabaseData().getPreparedStatement(sql);

        return getDatabaseData().updateData(sql);
    }

    public int editPrinter(Printer printer) throws SQLException {
            String sql = "update " + getTablePrinter() + " set ip_address_printer = ?, nama_printer = ?"+
                " where id = ?";

            PreparedStatement ps = getDatabaseData().getPreparedStatement(sql);

            ps.setInt(3, printer.getId());
            ps.setString(1, printer.getIp_address_printer());
            ps.setString(2, printer.getNama_printer());

            return getDatabaseData().updateData(ps);

    }

    /**
     * @return the databaseData
     */
    public DatabaseData getDatabaseData() {
        return databaseData;
    }

    /**
     * @param databaseData the databaseData to set
     */
    public void setDatabaseData(DatabaseData databaseData) {
        this.databaseData = databaseData;
    }

    /**
     * @return the tableuser
     */
    public String getTablePrinter() {
        return tablePrinter;
    }

    /**
     * @param tablePrinter the tablePrinter to set
     */
    public void setTablePrinter(String tablePrinter) {
        this.tablePrinter = tablePrinter;
    }

    /**
     * @return the totalRow
     */
    public int getTotalRow() {
        return totalRow;
    }
}
